package com.fruitshop.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fruitshop.dto.OrderCreateDTO;
import com.fruitshop.service.OrderService;
import com.fruitshop.vo.OrderVO;
import com.fruitshop.vo.ResultVO;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

/**
 * 订单控制器
 */
@Slf4j
@RestController
@RequestMapping("/order")
@RequiredArgsConstructor
public class OrderController {
    
    private final OrderService orderService;
    
    /**
     * 创建订单
     *
     * @param request        HTTP请求
     * @param orderCreateDTO 订单创建数据
     * @return 订单编号
     */
    @PostMapping("/create")
    public ResultVO<String> create(HttpServletRequest request, @RequestBody @Valid OrderCreateDTO orderCreateDTO) {
        Long userId = (Long) request.getAttribute("userId");
        String orderNo = orderService.create(userId, orderCreateDTO);
        return ResultVO.success(orderNo);
    }
    
    /**
     * 支付订单
     *
     * @param request HTTP请求
     * @param orderNo 订单编号
     * @return 支付结果
     */
    @PostMapping("/pay")
    public ResultVO<Boolean> pay(HttpServletRequest request, @RequestParam String orderNo) {
        Long userId = (Long) request.getAttribute("userId");
        Boolean result = orderService.pay(userId, orderNo);
        return ResultVO.success(result);
    }
    
    /**
     * 取消订单
     *
     * @param request HTTP请求
     * @param orderNo 订单编号
     * @return 取消结果
     */
    @PostMapping("/cancel")
    public ResultVO<Boolean> cancel(HttpServletRequest request, @RequestParam String orderNo) {
        Long userId = (Long) request.getAttribute("userId");
        Boolean result = orderService.cancel(userId, orderNo);
        return ResultVO.success(result);
    }
    
    /**
     * 确认收货
     *
     * @param request HTTP请求
     * @param orderNo 订单编号
     * @return 确认结果
     */
    @PostMapping("/confirm")
    public ResultVO<Boolean> confirm(HttpServletRequest request, @RequestParam String orderNo) {
        Long userId = (Long) request.getAttribute("userId");
        Boolean result = orderService.confirm(userId, orderNo);
        return ResultVO.success(result);
    }
    
    /**
     * 获取订单详情
     *
     * @param request HTTP请求
     * @param orderNo 订单编号
     * @return 订单详情
     */
    @GetMapping("/detail")
    public ResultVO<OrderVO> detail(HttpServletRequest request, @RequestParam String orderNo) {
        Long userId = (Long) request.getAttribute("userId");
        OrderVO orderVO = orderService.getDetail(userId, orderNo);
        return ResultVO.success(orderVO);
    }
    
    /**
     * 分页查询订单列表
     *
     * @param request  HTTP请求
     * @param status   订单状态
     * @param page     页码
     * @param pageSize 页大小
     * @return 订单分页列表
     */
    @GetMapping("/list")
    public ResultVO<Page<OrderVO>> list(
            HttpServletRequest request,
            @RequestParam(required = false) Integer status,
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer pageSize) {
        Long userId = (Long) request.getAttribute("userId");
        Page<OrderVO> orderPage = orderService.pageList(userId, status, page, pageSize);
        return ResultVO.success(orderPage);
    }
} 